<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\HomeUserStow;
use App\Model\Goods;
use App\Http\Controllers\Api\IndexApi;
use App\Http\Controllers\Api\SearchApi;
use App\Http\Controllers\Home\ShowLinksController;

/**
 * [关于收藏的Controller]
 * @author [cjinhui] <[514384598]>
 */
class HomeGoodsStowController extends Controller
{
    /**
     * 显示收藏页面
     */
    public function homeGoodsStow(Request $request)
    {
    	$user = session('Homeuser-data');

    	$goods = [];

    	if (!empty($user)) {

    		//查询出该用户所有收藏商品的商品ID
    		$gid = HomeUserStow::select('goods_id')->where('user_id', $user['id'])->get()->toArray();

    		foreach ($gid as $v) {
    			$goods[] = Goods::find($v['goods_id'])->toArray();
    		}


    	}
          
        //查询友情链接数据
        $link = new ShowLinksController;
        $linksArray = $link->index();

        //查询站点信息
        $sate = IndexApi::getSate();

        //实例化搜索类
        $search = new SearchApi('abc');

          //查询热搜词
        $Hotsearch = $search->getHot();

    	return view('Home.goods-stow', ['user' => $user, 'goods' => $goods, 'sate' => $sate, 'linksArray' => $linksArray, 'HotSearch' => $Hotsearch]);

    }


    /**
     * 处理删除用户收藏的商品
     */
    public function homeDelGoodsStow(Request $request)
    {
    	$id = $request->id;

    	$res = HomeUserStow::where('goods_id', $id)->delete();

    	if (!$res) {
    		return [
    			'msg' => '删除失败',
    			'status' => 404
    		];
    	}


    	return [
			'msg' => '删除成功',
			'status' => 200
		];
    }

    public function homeAddGoodsStow(Request $request)
    {

        $data['goods_id'] = $request->goods_id;

        $data['user_id'] = session('Homeuser-data')['id'];

        $res = HomeUserStow::insert($data);

        if ( !$res ) {
            return ['status' => '404', 'message' => '收藏失败！'];
        }

        return ['status' => '200', 'message' => '收藏成功!'];


    }



}
